package com.mercury.design.staregy.factory;


import com.mercury.core.manager.RedisManager;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author mercury
 */
@Component
public class BaseApiFactory {

    @Resource
    public RedisManager redisManager;

    /**
     * 模版替换方法
     *
     * @param content 替换内容
     * @param map     map
     * @return 替换后的内容
     */
    public String renderString(String content, Map<String, Object> map) {
        Set<Map.Entry<String, Object>> sets = map.entrySet();
        for (Map.Entry<String, Object> entry : sets) {
            String regex = "\\$\\{" + entry.getKey() + "}";
            Pattern pattern = Pattern.compile(regex);
            Matcher matcher = pattern.matcher(content);
            content = matcher.replaceAll(String.valueOf(entry.getValue()));
        }
        return content;
    }


}
